package com.jieao.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jieao.content.domain.ArticleComment;
import com.jieao.content.dto.ArticleCommentDto;
import com.jieao.content.mapper.ArticleCommentMapper;
import com.jieao.content.service.IArticleCommentService;
import com.jieao.framework.util.ShiroUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author weifanl
 * @Date2020/5/21 15:33
 **/
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleComment> implements IArticleCommentService {

    @Override
    public List<ArticleCommentDto> pageCommentList(ArticleComment article) {
        List<ArticleCommentDto> commentList = baseMapper.pageCommentList(article);
        return commentList;
    }

    @Override
    public List<ArticleCommentDto> selectCommentList(Long articleId) {
        List<ArticleCommentDto> commentList = baseMapper.selectCommentList(articleId, ShiroUtils.getUserId());
        return createTree(commentList);
    }

    @Override
    public void insertComment(ArticleComment comment) {
        comment.setUserId(ShiroUtils.getUserId());
        comment.setUserName(ShiroUtils.getSysUser().getUserName());
        baseMapper.insert(comment);
    }

    private List<ArticleCommentDto> createTree(List<ArticleCommentDto> commentList){
        List<ArticleCommentDto> resultList = new ArrayList<ArticleCommentDto>();

        for(ArticleCommentDto dto : commentList){
            if(null == dto.getPid()){
                resultList.add(dto);
            }else{
                for(ArticleCommentDto dto1 : resultList){
                    if(dto.getPid().equals(dto1.getId())){
                        dto1.getChlidren().add(dto);
                    }
                }
            }
        }
        return resultList;
    }
}
